﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using ATW.MT.BUS;
using BUS;

namespace ATW.MT.BaseManage
{
    public partial class BidManage : System.Web.UI.Page
    {
        bus_T_DemandInformation info = new bus_T_DemandInformation();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }
        private void BindData()
        {//绑定
            string bid = Request.QueryString["bid"];
            try
            {
                string dateEnd = info.GetDemandCloseDate(bid);
                DateTime _dateEnd = new DateTime();
                DateTime.TryParse(dateEnd, out _dateEnd);
                if (_dateEnd > DateTime.Now)
                {
                    Tips("未到截标时间，不能查看竞标详情！");
                    return;
                }
                if (!CommonClass.common.filterChar(bid))
                {
                    return;
                }
                DataTable dt = info.BidQuery(bid);
                dt.Columns.Add("totalScore");
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        dt.Rows[i]["totalScore"] = loadInfo(dt.Rows[i]["tdino"].ToString(), dt.Rows[i]["TDIBNO"].ToString()).TotalScore;
                    }
                }
                DataView dv = dt.DefaultView;
                dv.Sort = "totalScore desc";
                dt = dv.ToTable();
                rpList.DataSource = dt;
                rpList.DataBind();
            }
            catch (Exception ex)
            {

            }
        }
        protected void lbnBidDetail_Click(object sender, EventArgs e)
        {//展开详情
            LinkButton lbn = (LinkButton)sender;
            string tdibno = lbn.CommandArgument;
            try
            {
                HiddenField hdf = (HiddenField)lbn.Parent.FindControl("hdfTDINO");
                Control trDetail = (Control)lbn.Parent.FindControl("trDetail");
                BidScore bs = loadInfo(hdf.Value, tdibno);
                LinkButton btnHid = (LinkButton)lbn.Parent.FindControl("lbnBidDetailHide");
                lbn.Visible = false;
                btnHid.Visible = true;
                trDetail.Visible = true;

                Repeater rpSub = (Repeater)lbn.Parent.FindControl("rpSub");
                Label lblPay = (Label)lbn.Parent.FindControl("lblPay");
                Label lblScore = (Label)lbn.Parent.FindControl("lblScore");
                Label lblSum = (Label)lbn.Parent.FindControl("lblSum");
                Label lblMemo = (Label)lbn.Parent.FindControl("lblMemo");
                if (rpSub != null && lblPay != null && lblScore != null && lblSum != null)
                {
                    DataTable dt = info.BidDetailQuery(hdf.Value);
                    if (dt != null && dt.Rows.Count > 0)
                    {
                        decimal _sum = 0;
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            decimal _t = 0;
                            if (dt.Rows[i]["total"].ToString() != "")
                            {
                                decimal.TryParse(dt.Rows[i]["total"].ToString(), out _t);
                                _sum += _t;
                            }
                        }
                        lblSum.Text = Math.Round(_sum, 2).ToString();
                    }
                    lblPay.Text = dt.Rows[0]["tdibpaytype"].ToString();
                    lblScore.Text = "报价得分：" + bs.PriceWeight +
                        " 支付方式得分：" + bs.PayWayWeight +
                        " 交货得分：" + bs.DeliverWeight +
                        " 技术得分：" + bs.TechnologyWeight +
                        " 总分：" + bs.TotalScore;
                    lblMemo.Text = bs.Memo == "" ? "无" : bs.Memo;
                    rpSub.DataSource = dt;
                    rpSub.DataBind();
                }

            }
            catch (Exception ex)
            {

            }
        }
        protected void lbnBidDetailHide_Click(object sender, EventArgs e)
        {//收起
            LinkButton lbn = (LinkButton)sender;
            LinkButton lbnShow = (LinkButton)lbn.Parent.FindControl("lbnBidDetail");
            Control trDetail = (Control)lbn.Parent.FindControl("trDetail");
            if (trDetail != null && trDetail.Visible)
            {
                trDetail.Visible = false;
            }
            lbnShow.Visible = true;
            lbn.Visible = false;
        }
        private BidScore loadInfo(string tdino, string tdibno)
        {//调用计算得分
            List<BidScore> list = new CommonHelp().GetScore(tdino);
            if (list.Count > 0)
            {
                foreach (BidScore i in list)
                {
                    if (i.tdibNo == tdibno)
                    {
                        return i;
                    }
                }
            }
            return new BidScore();
        }
        protected void lbnBidBingo_Click(object sender, EventArgs e)
        {//中标
            LinkButton lbn = (LinkButton)sender;
            string tdibno = lbn.CommandArgument;
            try
            {
                HiddenField hdf = (HiddenField)lbn.Parent.FindControl("hdfTDINO");
                if (info.DemandClose(hdf.Value, tdibno))
                {
                    Tips("操作成功！");
                    BindData();
                }
                else
                {
                    Tips("操作失败！");
                }
            }
            catch (Exception ex)
            {

            }
        }
        private void Tips(string s)
        {
            StringBuilder strScript = new StringBuilder();
            strScript.Append(@"<script language=javascript>
                var divTips=new jSharp.popUpDiv('divMsg');
                document.getElementById('msgContent').innerHTML='" + s + @"';
                divTips.Show();
                var t=setTimeout(function(){
                    divTips.Close();
                    clearTimeout(t);
                },1000);
            </script>");
            this.ClientScript.RegisterStartupScript(this.GetType(), "tips", strScript.ToString());
        }
    }
}